import React, { useState, useEffect, useCallback } from 'react';
import { getSysDepartmentFindList, getSysUserSelected } from '../../../../../../Api/listUrl';
import {CostNature} from '../../../enum';
import { Form, Button, Select, Row, Col, message, DatePicker } from "antd";
import moment from 'moment';

const { Option } = Select;
const { RangePicker } = DatePicker;

/**
 * @description: 材料交易单 -- 出库 -- 搜索组件
 * @author: xiaoxujs
 * @updata: xiaoxujs（2021-09-18） 创建组件
 */

const SelectModule = ({ handleSelectModuleOk, handleSelectModuleReset }) => {
    const [form] = Form.useForm()
    const { resetFields } = form;
    const [departmentList, setDepartmentList] = useState([]); //部门list
    const [userList, setUserList] = useState([]); //人员list
    const userFun = useCallback(
        () => {
         ;(async () => {
            const {code, msg, data} = await getSysUserSelected();
            if(code === '20000') {
                setUserList(data);
            }else{
                message.error(msg);
            }
         })(); 
        },
        [],
    )
    const departmentFun = useCallback(
        () => {
            ; (async () => {
                const { code, msg, data } = await getSysDepartmentFindList();
                if (code === '20000') {
                    setDepartmentList(data);
                } else {
                    message.error(msg);
                }
            })();
        },
        [],
    )
    useEffect(() => {
        departmentFun();
        userFun();
    }, [departmentFun, userFun])
    const handleReset = () => {
        resetFields()
        handleSelectModuleReset()
    }
    const onFinish = (values) => {
        if (values.time) {
            values.happenStartDate = moment(values.time[0]).format('YYYY-MM-DD');
            values.happenEndDate = moment(values.time[1]).format('YYYY-MM-DD');
            delete values.time;
        }
        handleSelectModuleOk(values)
    };
    return <Form
        {...layout}
        name="basic"
        initialValues={{ remember: true }}
        onFinish={onFinish}
        form={form}
    >
        <Row>
            <Col span={6}>
                <Form.Item name="time" label="发生时间" hasFeedback>
                    <RangePicker />
                </Form.Item>
            </Col>
            <Col span={6}>
                <Form.Item name="nature" label="成本性质" hasFeedback>
                    <Select placeholder="请选择">
                        {
                            CostNature.map((item, index) => <Option value={index} key={item + index}> {item} </Option>)
                        }
                    </Select>
                </Form.Item>
            </Col>

            <Col span={6}>
                <Form.Item name="assumeDep" label="承担部门" hasFeedback>
                    <Select placeholder="请选择">
                        {
                            departmentList.map(item => <Option value={item.id} key={item.id}> {item.name} </Option>)
                        }
                    </Select>
                </Form.Item>
            </Col>
            <Col span={6}>
                <Form.Item name="assumeMain" label="承担人员" hasFeedback>
                    <Select placeholder="请选择">
                        {
                            userList.map(item => <Option value={item.id} key={item.id}> {item.name} </Option>)
                        }
                    </Select>
                </Form.Item>
            </Col>

        </Row>
        <Row>
            <Col span={24} className="bottom_button_box">
                <Form.Item wrapperCol={{ offset: 0, span: 24 }}>
                    <Button
                        type="primary"
                        htmlType="submit"
                        style={{
                            height: '38px',
                            width: '78px',
                            borderRadius: '4px'
                        }}
                    >
                        搜索
                    </Button>
                    <Button
                        onClick={handleReset}
                        style={{
                            height: '38px',
                            width: '78px',
                            borderRadius: '4px'
                        }}
                    >重置</Button>
                </Form.Item>
            </Col>
        </Row>
    </Form>
}
const layout = {
    labelCol: { span: 8 },
    wrapperCol: { span: 14 },
};
export default SelectModule;